package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.BackgroundAdapter;
import flc.ast.databinding.ActivityFigureBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.constant.Extra;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;

public class FigureActivity extends BaseAc<ActivityFigureBinding> {

    public static Bitmap bitmap;
    private BackgroundAdapter backgroundAdapter;
    public static final int ADD_FIGURE_CODE = 220;


    @Override
    protected int onCreate() {
        return R.layout.activity_figure;
    }

    @Override
    protected void initView() {
        mDataBinding.ivFigureBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivAddFigure.setOnClickListener(this);
        mDataBinding.ivFigureSave.setOnClickListener(this);
        mDataBinding.rvBackgroundList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        backgroundAdapter = new BackgroundAdapter();
        mDataBinding.rvBackgroundList.setAdapter(backgroundAdapter);
        backgroundAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAddFigure://添加人像
                Intent intent = new Intent(this, AlbumChooseActivity.class);
                intent.putExtra(Content.Add, "Add");
                startActivityForResult(intent, ADD_FIGURE_CODE);
                break;
            case R.id.ivFigureSave://保存
                mDataBinding.ivStickerViewHuman.setShowHelpToolFlag(false);
                saveImg();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        mDataBinding.ivFigureImg.setImageResource(backgroundAdapter.getItem(position));
    }

    @Override
    protected void initData() {
        //背景图片
        List<Integer> list = new ArrayList<>();
        list.add(R.drawable.aa01);
        list.add(R.drawable.aa02);
        list.add(R.drawable.aa03);
        list.add(R.drawable.aa04);
        list.add(R.drawable.aa05);
        list.add(R.drawable.aa06);
        list.add(R.drawable.aa07);
        list.add(R.drawable.aa08);
        list.add(R.drawable.aa09);
        list.add(R.drawable.aa10);
        list.add(R.drawable.aa11);
        list.add(R.drawable.aa12);
        backgroundAdapter.setList(list);
        //设置图片数据
        if (bitmap == null) return;
        showDialog(getString(R.string.loading));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissDialog();
                mDataBinding.ivStickerViewHuman.addBitImage(bitmap);
                mDataBinding.ivFigureImg.setImageResource(list.get(0));
            }
        }, 500);
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlSaveImg);
                        //保存到相册
                        File file = ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext(file.getPath());
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        PicPreviewActivity.imgPath=path;
                        startActivity(PicPreviewActivity.class);
                    }
                });
            }
        }, 1000);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == ADD_FIGURE_CODE) {
                mDataBinding.ivStickerViewHuman.addBitImage(bitmap);
            }
        }
    }
}